import { AppThemeEnum } from '@/enum/global.enum';
import { create } from 'zustand';
import { persist } from 'zustand/middleware'; // 新增持久化中间件

import { immer } from 'zustand/middleware/immer';
// 定义状态类型
type GlobalState = {
  Layout: 'default' | 'horizontal';
  updateLayout: (layout: 'default' | 'horizontal') => void;
  theme: AppThemeEnum;
  updateTheme: (theme: AppThemeEnum) => void;
  collapsed: boolean;
  updateCollapsed: (collapsed: boolean) => void;
  collapsedWidth: number;
  updateCollapsedWidth: (collapsedWidth: number) => void;
  primaryColor: string;
  updatePrimaryColor: (primaryColor: string) => void;
  Langeuage: string;
  updateLanguage: (lang: string) => void;
  isWatermark: boolean;
  updateIsWatermark: (isWatermark: boolean) => void;
};

const useGlobalStore = create<GlobalState>()(
  persist(
    immer<GlobalState>((set) => ({
      Layout: 'default',
      updateLayout: (layout: 'default' | 'horizontal') =>
        set({ Layout: layout }),
      theme: AppThemeEnum.light,
      updateTheme: (theme: AppThemeEnum) => set({ theme }),
      collapsed: false,
      updateCollapsed: (collapsed: boolean) => set({ collapsed: collapsed }),

      primaryColor: '#1890ff',
      updatePrimaryColor: (primaryColor: string) =>
        set({ primaryColor: primaryColor }),

      collapsedWidth: 40,
      updateCollapsedWidth: (collapsedWidth: number) =>
        set({ collapsedWidth: collapsedWidth }),

      Langeuage: navigator.language || 'zh-CN',
      updateLanguage: (Lang: string) => set({ Langeuage: Lang }),
      isWatermark: false,
      updateIsWatermark: (isWatermark: boolean) => set({ isWatermark: isWatermark }),
    })),
    {
      name: 'global',
    },
  ),
);

export default useGlobalStore;
